package com.lightingcolorfiltertest.test.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.lightingcolorfiltertest.test.R;

/**
 * @DESC:   图片的点击状态
 * @Author: Jason
 * @Date: 16/8/23
 * @Time: 下午5:10
 */
public class PressImageView extends View {

    private Paint mPaint;
    private Bitmap mBitmap;
    private int x,y; // 坐标
    private boolean isClick;

    public PressImageView(Context context) {
        this(context, null);
    }

    public PressImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);

        x = context.getResources().getDisplayMetrics().widthPixels / 2 - mBitmap.getWidth() / 2;
        y = context.getResources().getDisplayMetrics().heightPixels / 2 - mBitmap.getHeight() / 2;

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (isClick) {
                    // 如果已经被点击了则点击时设置颜色过滤为空还原本色
                    isClick = false;
                    mPaint.setColorFilter(null);
                } else {
                    // 如果未被点击则点击时设置颜色过滤后为黄色
                    isClick = true;
                    mPaint.setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0X00FFFF00));

                }

                invalidate();
            }
        });


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mBitmap, x, y, mPaint);
    }
}
